package org.micro.frame.es.demo;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.After;
import org.junit.Before;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Base
 *
 * @author lry
 */
public class ElasticsearchBase {

    protected RestHighLevelClient client = null;
    protected Map<String, Object> jsonMap = new HashMap<>();

    @Before
    public void start() throws Exception {
        HttpHost httpHost = HttpHost.create("127.0.0.1:9200");
        RestClientBuilder builder = RestClient.builder(httpHost);
        this.client = new RestHighLevelClient(builder);
        System.out.println("启动结果：" + client.ping());

        jsonMap.put("user", "kimchy");
        jsonMap.put("postDate", new Date());
        jsonMap.put("message", "trying out Elasticsearch");
    }

    @After
    public void close() {
        try {
            if (client != null) {
                client.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
